#ifndef VIDEO_DEVICE_H
#define VIDEO_DEVICE_H

#include <QObject>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <linux/types.h>
#include <linux/videodev2.h>
#include <setjmp.h>
#include "include.h"
#include <QImage>
#include <QFile>


typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef unsigned long  DWORD;


class video_device : public QObject
{
    Q_OBJECT
public:
    explicit video_device(const char* video_name,QObject *parent = 0);
    int init_video_device(const char *devname);
    int yuyv_2_rgb888(void);
    int video_grab(void);
    int close_device(void);
    void screenshot(void);
    int setframerate(unsigned char rate);
    void saveimage(QImage& image,QString pathname);
   // unsigned char fps;
    QImage frame;
signals:
    
public slots:
private:

    int    fd_video;
    struct v4l2_capability   cap;
    struct v4l2_fmtdesc fmtdesc;
    struct v4l2_format fmt,fmtack;
    struct v4l2_streamparm setfps;
    struct v4l2_requestbuffers req;
    struct v4l2_buffer buf;
    enum   v4l2_buf_type type;

    struct buffer
    {
        void * start;
        unsigned int length;
    } * buffers;
    const char *devname;


    typedef struct tagBITMAPFILEHEADER{
         WORD bfType;              // the flag of bmp, value is "BM"
         DWORD    bfSize;          // size BMP file ,unit is bytes
         DWORD    bfReserved1;     // 0
         DWORD    bfReserved2;     // 0
         DWORD    bfOffBits;       // must be 54
    }BITMAPFILEHEADER;    //这个结构中占据14字节

    typedef struct tagBITMAPINFOHEADER{
         DWORD    biSize;            // must be 0x28
         DWORD    biWidth;           //
         DWORD    biHeight;          //
         WORD   biPlanes;          // must be 1
         WORD   biBitCount;        //
         DWORD    biCompression;     //
         DWORD    biSizeImage;       //
         DWORD    biXPelsPerMeter;   //
         DWORD    biYPelsPerMeter;   //
         DWORD    biClrUsed;         //
         DWORD    biClrImportant;    //
    }BITMAPINFOHEADER;    //这个结构占据40个字节

    BITMAPFILEHEADER  bf; //bmp文件头
    BITMAPINFOHEADER  bi; //bmp信息头
        FILE * fp;

        fd_set fds ;
        struct timeval tv;


        void bmpheader();
};

#endif // VIDEO_DEVICE_H
